<?php
namespace app\car\controller;
use app\AdminRest;

use app\ApiRest;
use app\car\model\ArticleCate;
use app\car\model\ArticleEvaluate;
use app\car\model\ArticleLabelText;
use app\car\model\ArticleList;
use app\car\model\ArticleStar;
use app\diy\model\DiyModel;
use app\shop\model\IndexCompany;
use think\App;
use think\facade\Db;


class IndexArticle extends ApiRest
{

    protected $model;

    protected $cate_model;

    protected $star_model;

    protected $evaluate_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model  = new ArticleList();

        $this->cate_model  = new ArticleCate();

        $this->star_model  = new ArticleStar();

        $this->evaluate_model  = new ArticleEvaluate();

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:分类列表
     */
    public function cateList(){

        $input = $this->_param;

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->cate_model->where($dis)->order('top desc,id desc')->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-02 15:56
     * @功能说明:文章列表
     */
    public function articleList(){
        //获取参数
        $input = $this->_param;

        $data  = $this->articleListApi($input);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-03 15:39
     * @功能说明:员工文章列表
     */
    public function staffArticleList(){

        $input = $this->_param;

        $input['staff_id'] = $this->getUserId();

        $data  = $this->articleListApi($input);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:58
     * @功能说明:文章列表
     */
    public function articleListApi($input){

        $staff_id = $input['staff_id'];

        $company_id = $this->model->getUserTopCompany($staff_id);

        $dis = [];

        $dis[] = ['a.status','=',1];

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['a.title','like','%'.$input['title'].'%'];
        }

        $day  = -99999999;

        $sort = 'a.top desc,a.id desc';

        if(!empty($input['cate_id'])){

            $cate_info = $this->cate_model->dataInfo(['id'=>$input['cate_id']]);

            if($cate_info['type']==0){

                $day = $cate_info['day']*-1;

                $sort= 'a.total_num desc,a.id desc';

            }elseif ($cate_info['type']==1){

                $day = $cate_info['day']*-1;

            }else{

                $dis[] = ['a.cate_id','=',$input['cate_id']];
            }


        }
        //标签搜索
        if(!empty($input['label_id'])){

            $lable_text_model = new ArticleLabelText();

            $article_id = $lable_text_model->where(['label_id'=>$input['label_id']])->column('article_id');

            $dis[] = ['a.id','in',$article_id];

        }

        $where[] = ['a.is_public','=',1];

        $where[] = ['b.user_id','=',$staff_id];

        $where[] = ['c.user_id','=',$company_id];

        $data = $this->model->indexDataList($dis,$day,$sort,$where,10);

        $user_name   = lbUserName($staff_id);

        $user_avatar = lbUserAvatar($staff_id);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['create_time'] = lb_friendly_date($v['create_time']);

                $star = $this->star_model->dataInfo(['user_id'=>$this->getUserId(),'article_id'=>$v['id']]);

                $v['is_star']     = !empty($star)?1:0;

                $v['user_name']   = $user_name;

                $v['user_avatar'] = $user_avatar;

            }

        }

        return $data;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-06-11 14:59
     * @功能说明:商城首页diy版本
     */
    public function articleListDiy(){
        //获取diy的数据
        $diy_data = DiyModel::where([['status', '=', 1],['uniacid', '=', $this->_uniacid]])->find();
        //获取商城的
        $page_data= json_decode($diy_data['page'], true)[50] ?? [];

        //判断有没有diy过
        if(empty($page_data)|| (array_key_exists('list',$page_data) && empty($page_data['list']))){

            $page_data = longbing_default_Page(50);

        }
        //获取参数
        $input    = $this->_param;
        //获取数据
        foreach ($page_data['list'] as &$value){
            //获取商品
            if($value['type']=='carArticleList'&&empty($value['data']['dataList'])){

                $data  = $this->articleListApi($input);

                $value['data']['dataList']  = $data;

            }

        }

        return $this->success($page_data);
    }



    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:文章详情
     */
    public function articleInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        $data['user_name']   = lbUserName($input['staff_id']);

        $data['user_avatar'] = lbUserAvatar($input['staff_id']);

        $data['create_time'] = lb_friendly_date($data['create_time']);

        $star = $this->star_model->dataInfo(['user_id'=>$this->getUserId(),'article_id'=>$data['id']]);

        $data['is_star']     = !empty($star)?1:0;
        //点赞列表
        $data['star_list'] = $this->star_model->dataList(['a.article_id'=>$input['id']]);

        $this->model->where(['id'=>$input['id']])->update(['read_num'=>Db::raw('read_num+1')]);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-24 11:35
     * @功能说明:评价列表
     */
    public function evaluateList(){

        $input = $this->_param;

        $dis = [

            'a.article_id' => $input['article_id'],

            'a.status'     => 1,

        ];

        $data = $this->evaluate_model->dataList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-24 14:39
     * @功能说明:添加评论
     */
    public function evaluateAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['user_id'] = $this->getUserId();

        $res = $this->evaluate_model->dataAdd($input);

        $res = $this->model->where(['id'=>$input['article_id']])->update(['ev_num'=>Db::raw('ev_num+1'),'total_num'=>Db::raw('total_num+1')]);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-24 14:40
     * @功能说明:点赞 取消点赞
     */
    public function starUpdate(){

        $input = $this->_input;

        $res = $this->star_model->starUpdate($input['article_id'],$this->getUserId(),$this->_uniacid);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:添加文章
     */
    public function articleAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['create_user'] = 2;

        $input['is_user'] = 1;

        $input['user_id'] = [$this->getUserId()];

        $data = $this->model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-03 15:41
     * @功能说明:编辑文章
     */
    public function articleUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']

        ];

        $info = $this->model->dataInfo($dis);

        if($info['is_user']==0){

            $this->errorMsg('只有自己创建的文章才能编辑哦');
        }

        $input['uniacid'] = $this->_uniacid;

        $input['create_user'] = 2;

        $input['user_id'] = [$this->getUserId()];

        $data = $this->model->dataUpdate($dis,$input);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-03 15:41
     * @功能说明:编辑文章
     */
    public function articleStatusUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']

        ];

        $input['uniacid'] = $this->_uniacid;

        $data = $this->model->dataUpdate($dis,$input);

        return $this->success($data);


    }

















}
